Palindrome Linked List
Question
Given a singly linked list, determine if it is a palindrome.
Analysis
判断一个链表是否为回文数
- 判断链表节点个数是奇数还是偶数
- 偶数的情况下,两个指针,p1一次走一步,并将路过的元素都压栈,p2一次走两步,当p2为null时,p1刚好指向链表中点,此时p1接着向下,元素与依次弹栈的元素进行对比,假如不等,则返回false,否则返回true
- 奇数的情况下,先将p2先后挪一位,其余相同
Code
|
|
Valid Palindrome
Question
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,"A man, a plan, a canal: Panama"
is a palindrome."race a car"
is not a palindrome.
Analysis
利用StringBuilder将所有符合条件的字符复制至salpha,之后判断salpha大小,利用toUpperCase
将所有的字符转换为大写后判断字符是否相等。
Code
|
|
Palindrome Partitioning
Question
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = “aab”,
Return
|
|
Analysis
DFS
- 从头开始遍历字符串,利用i循环截取不同长度的子串,当截取字符串为回文串时将其加入item,以当前为起点进入下一次调用。
- 当start等于字符串长度的时候,将item加入result,并且调用结束后逐步将item清空。
- List初始化方法:List
- > res = new ArrayList<>()
Code
|
|